package com.kong.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.seed.mybatis.core.PageInfo;
import org.seed.mybatis.core.query.Query;
import org.seed.mybatis.core.query.param.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.burgeon.commons.react.Result;
import com.kong.dao.entity.GenerateConfig;
import com.kong.service.GenerateConfigService;

/**
 * 生成配置
 * @author glin
 * @date 2024-04-16
 */
@RestController
@RequestMapping("generate-config")
@Tag(name = "生成配置")
public class GenerateConfigController {

    @Autowired
    private GenerateConfigService generateConfigService;

    /**
     * 分页查询
     *
     * @param param 分页参数
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    @Parameters({
            @Parameter(name = "pageIndex", description = "当前页码"),
            @Parameter(name = "pageSize", description = "每页记录数")
    })
    public Result<PageInfo<GenerateConfig>> page(PageParam param) {
        Query query = param.toQuery();
        PageInfo<GenerateConfig> pageInfo = generateConfigService.page(query);
        return Result.ok(pageInfo);
    }
    
    /**
     * 新增记录
     *
     * @param generateConfig
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "新增记录")
    public Result<String> save(@RequestBody GenerateConfig generateConfig) {
        generateConfigService.saveIgnoreNull(generateConfig);
        // 返回添加后的主键值
        return Result.ok(generateConfig.getId());
    }
    
    /**
     * 修改记录
     *
     * @param generateConfig 表单数据
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "修改记录")
    public Result<Void> update(@RequestBody GenerateConfig generateConfig) {
        GenerateConfig store = generateConfigService.getById(generateConfig.getId());
        if (store == null) {
            return Result.errorMessageOnly("无数据记录");
        }

        generateConfigService.updateIgnoreNull(generateConfig);
        return Result.ok();
    }
     
    /**
     * 删除记录
     *
     * @param id 主键id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除记录")
    public Result<Void> delete(String id) {
        generateConfigService.deleteById(id);
        return Result.ok();
    }


    /**
     * 根据ID查询
     *
     * @param id 主键id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "根据ID查询")
    public Result<GenerateConfig> get(String id) {
        GenerateConfig generateConfig = generateConfigService.getById(id);
        return Result.ok(generateConfig);
    }
    
}